package cn.com.huzo.webapp.controller.master;

import huzofw.common.web.controller.BaseFormController;
import cn.com.huzo.model.entities.master.DepartmentEntity;
import huzofw.model.entities.viewmodel.TreeNode;
import cn.com.huzo.service.master.DepartmentManager;
import huzofw.model.entities.identity.IdentityUserEntity;
import huzofw.service.security.MemberHasRoleManager;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 部门信息控制器
 *
 * @author 吴旭
 */
@Controller
public class DepartmentController extends BaseFormController {

    // <editor-fold defaultstate="collapsed" desc="属性">
    private final String RoleNo = "0001";  // 系统管理员角色

    @Resource(name = "DepartmentManager")
    private DepartmentManager _manager;

    @Resource(name = "MemberHasRoleManager")
    private MemberHasRoleManager _memberHasRoleManager;
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="画面处理部分">
    /**
     * 跳转到部门编辑画面
     * <p>
     * @return
     */
    @RequestMapping("master/department/edit.html")
    public ModelAndView redirect() {
        ModelAndView view = new ModelAndView();
        view.setViewName("master.department.edit.def");
        return view;
    }

    /**
     * 弹出部门选择画面
     * <p>
     * @return
     */
    @RequestMapping("master/department/choice.html")
    public ModelAndView redirectChoiceWnd() {
        ModelAndView view = new ModelAndView();
        view.setViewName("popup.master.department.choice.def");
        return view;
    }
    // </editor-fold>

    // <editor-fold defaultstate="collapsed" desc="数据处理部分">
    /**
     * 取得部门树节点数据。
     *
     * @param key 部门标识
     * @return 部们数据对象。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/department/fetchByKey.html",
            method = RequestMethod.GET)
    public @ResponseBody
    DepartmentEntity fetchByKey(@RequestParam Long key) throws Exception {
        return _manager.fetchByKey(key);
    }

    /**
     * 取得部门数节点数据。
     *
     * @param parentKey 所属部门标识
     * @param isPopupWnd 是否为弹出选择窗口
     * @param session
     * @return 该部门下的子部们数据节点列表。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/department/tree.html",
            method = RequestMethod.GET)
    @ResponseBody
    public List<TreeNode> getDeptTreeNode(@RequestParam Long parentKey, @RequestParam Integer isPopupWnd, HttpSession session) throws Exception {
        IdentityUserEntity identityUser = (IdentityUserEntity) session.getAttribute("IdentityUser");
        Map<String, Object> filters = new HashMap();
        filters.put("UserName", identityUser.getUserName());
        filters.put("RoleNo", RoleNo);
        Integer hasCheckRole = _memberHasRoleManager.fetchCountByRoleNoAndUserName(filters);
        boolean filter = (hasCheckRole > 0);
        
        List<TreeNode> _listDeptTree = _manager.fetchTreeNodeByParentKey(parentKey, isPopupWnd, filter);
        return _listDeptTree;
    }

    /**
     * 保存部门信息数据。
     * <p>
     * @param instance 要保存的部门信息对象。
     * @return 新增记录主键.
     * @throws Exception
     */
    @RequestMapping(value = "master/department/save.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Long save(@RequestBody final DepartmentEntity instance) throws Exception {
        Long rowsAffected;

        if (instance.getDepartmentId() > 0) { //编辑
            rowsAffected = (long) _manager.update(instance); // 返回影响行数
        } else {//新增
            rowsAffected = _manager.insert(instance);        // 返回主键
        }

        return rowsAffected;
    }

    /**
     * 更改所属目录。
     *
     * @param nodeParams 当前目录及父目录标识
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/department/changeParentCatalogue.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer changeParentCatalogue(@RequestBody final Map<String, Object> nodeParams) throws Exception {
        return _manager.changeParentCatalogue(nodeParams);
    }

    /**
     * 删除部门信息数据。
     *
     * @param instance 部门标识
     * @return 影响的行数。
     * @throws java.lang.Exception
     */
    @RequestMapping(value = "master/department/delete.html",
            method = RequestMethod.POST)
    public @ResponseBody
    Integer delete(@RequestBody DepartmentEntity instance) throws Exception {
        return _manager.delete(instance);
    }
    // </editor-fold>
}
